package vn.com.atm;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.com.atm.library.JSONParser;
import vn.com.atm.library.UserFunctions;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

public class AllATMActivity extends ListActivity{
	private ProgressDialog pDialog;
	String iAtmCode,iBankCode,iAddress;
    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();
 
    ArrayList<HashMap<String, String>> atmList;
 
    // url to get all products list
   
    // JSON Node names
    private static final String TAG_ATM = "atm";
    private static final String TAG_BANKCODE = "bankCode";
    private static final String TAG_ATMCODE= "atmCode";
    private static final String TAG_ADDRESS= "address";
 
    // products JSONArray
    JSONArray atm = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	super.onCreate(savedInstanceState);
    	
    	atmList = new ArrayList<HashMap<String, String>>();
    	setContentView(R.layout.activity_all_atm);
    	
    	 Intent in = getIntent();
    	 iAtmCode = in.getStringExtra(TAG_ATMCODE);
		 iBankCode = in.getStringExtra(TAG_BANKCODE);
		 iAddress = in.getStringExtra(TAG_ADDRESS);
    	new LoadATMList().execute();
    }
    
    class LoadATMList extends AsyncTask<String, String, String>
    {
    	 @Override
         protected void onPreExecute() {
             super.onPreExecute();
             pDialog = new ProgressDialog(AllATMActivity.this);
             pDialog.setMessage("Loading data. Please wait...");
             pDialog.setIndeterminate(false);
             pDialog.setCancelable(false);
             pDialog.show();
         }
    	@Override
		protected String doInBackground(String...  args) {
             // getting JSON string from URL
    		 UserFunctions userFunction = new UserFunctions();
    		 
    		
    		  
             JSONObject json = userFunction.searchATM(iBankCode, iAtmCode, iAddress, 0);
  
             // Check your log cat for JSON reponse
             Log.d("All ATM: ", json.toString());
  
             try {
                 // Checking for SUCCESS TAG
                 
  
                 if (json.getJSONArray(TAG_ATM) != null) {
                     // products found
                     // Getting Array of Products
                     atm = json.getJSONArray(TAG_ATM);
  
                     // looping through All Products
                     for (int i = 0; i < atm.length(); i++) {
                         JSONObject c = atm.getJSONObject(i);
  
                         // Storing each json item in variable
                         String bankCode = c.getString(TAG_BANKCODE);
                         String address = c.getString(TAG_ADDRESS);
  
                         // creating new HashMap
                         HashMap<String, String> map = new HashMap<String, String>();
  
                         // adding each child node to HashMap key => value
                         map.put(TAG_BANKCODE, bankCode);
                         map.put(TAG_ADDRESS, address);
  
                         // adding HashList to ArrayList
                         atmList.add(map);
                     }
                 } else {
                     // no products found
                     // Launch Add New product Activity
                     Intent i = new Intent(getApplicationContext(),
                             MainActivity.class);
                     // Closing all previous activities
                     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                     startActivity(i);
                 }
             } catch (JSONException e) {
                 e.printStackTrace();
             }
  
             return null;
		}
    	 protected void onPostExecute(String file_url) {
             // dismiss the dialog after getting all products
             pDialog.dismiss();
             // updating UI from Background Thread
             runOnUiThread(new Runnable() {
                 public void run() {
                     /**
                      * Updating parsed JSON data into ListView
                      * */
                     ListAdapter adapter = new SimpleAdapter(
                             AllATMActivity.this, atmList,
                             R.layout.list_item, new String[] { TAG_BANKCODE,
                            		 TAG_ADDRESS},
                             new int[] { R.id.pid, R.id.name });
                     // updating listview
                     setListAdapter(adapter);
                 }
             });
  
         }
    }
    
    
		
}
